package interceptor;

import io.jsonwebtoken.Claims;
import model.SysUsers;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor {

    //在handler执行前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle执行了");
        if(request.getRequestURI().contains("login")){
            return true;//放行
        }
//        HttpSession session = request.getSession();
//        String key  = (String) session.getAttribute("salt");
//        if (key==null){
//            key = "1";
//        }
        String token =  request.getHeader("token");
        if(token==null){//如果没有token
            token = "1";
        }
        Claims claims =  JwtUtil.checkJwt(token);
        System.out.println(claims);

        return true;

    }

    //在handler执行后，视图解析前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle执行了");
    }

    //在处理请后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion执行了");
    }
}
